﻿using blackhouse.BlackApart.Business.Apartments;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.BlackApart.WebNemo {
    public class NemoApartment : GroupRecord, IApartment {

        #region Fields

        private NemoApartmentType type;

        #endregion

        #region Properties

        private NemoApartmentType Type {
            get {
                if (this.type == null)
                    this.type = this.GetRecord<NemoApartmentType>("typ_apartamentu");
                return this.type;
            }
        }

        public int Id {
            get { return this.RecordId; }
        }

        public int NumberOfPeople {
            get {
                return this.Type.NumberOfPeople;
            }
            set { throw new NotImplementedException(); }
        }

        public IApartmentLocation Location {
            get {
                return this.Type.DefaultLocalization;
            }
            set { throw new NotImplementedException(); }
        }

        public float AreaSize {
            get {
                return this.Type.AreaSize;
            }
            set { throw new NotImplementedException(); }
        }

        public string Name {
            get {
                return this.GetString("nazwa");
            }
            set { throw new NotImplementedException(); }
        }

        public IApartmentGroup Group {
            get {
                return this.Type;
            }
            set { throw new NotImplementedException(); }
        }

        #endregion

        #region Constructors

        public NemoApartment() : base("APARTAMENTY", 0) { }

        public NemoApartment(int apartmentId) : base("APARTAMENTY", apartmentId) { }

        public NemoApartment(IGroup group, IGroupRecord record) : base(group, record) { }

        #endregion

        #region Methods

        public new bool Save() {
            return this.Save();
        }

        public IApartment[] GetAllFreeApartments(DateTime strtDate, DateTime endDate) {
            return this.GetAllFreeApartments(strtDate, endDate, 0);
        }

        public IApartment[] GetAllFreeApartments(DateTime strtDate, DateTime endDate, int apartmentGroupId) {
            IGroup group = blackhouse.WebNemo.Business.Factory.Instance().GetGroup("APARTAMENTY");
            GroupRecords apartmentRecords = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_ApartmentGetAllFree");
            q.AddProcedureParameter("StartDate", strtDate, System.Data.DbType.DateTime);
            q.AddProcedureParameter("EndDate", endDate, System.Data.DbType.DateTime);
            if (apartmentGroupId > 0)
                q.AddProcedureParameter("GroupId", apartmentGroupId, System.Data.DbType.Int32);
            q.AddCacheDependenceGroup("REZERWACJA_POZYCJE");
            return apartmentRecords.GetRecords<NemoApartment>(q);
        }

        public IApartment[] GetApartmentsByGroup(int groupId) {
            IGroup group = blackhouse.WebNemo.Business.Factory.Instance().GetGroup("APARTAMENTY");
            GroupRecords apartmentRecords = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_ApartmentsGetByType");
            q.AddWhereClause("typ_apartamentu", groupId);
            return apartmentRecords.GetRecords<NemoApartment>(q);
        }

        #endregion

    }
}
